package com.chonor.mall.service.impl;

import com.chonor.mall.dao.HomeDao;
import com.chonor.mall.domain.FlashPromotionProduct;
import com.chonor.mall.domain.HomeContentResult;
import com.chonor.mall.domain.HomeFlashPromotion;
import com.chonor.mall.mapper.FlashPromotionMapper;
import com.chonor.mall.mapper.HomeAdvertiseMapper;
import com.chonor.mall.mapper.ProductCategoryMapper;
import com.chonor.mall.mapper.ProductMapper;
import com.chonor.mall.model.*;
import com.chonor.mall.service.HomeService;
import com.chonor.mall.util.DateUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 首页内容管理Service实现类
 */
@Service
public class HomeServiceImpl implements HomeService {
    @Autowired
    private HomeAdvertiseMapper advertiseMapper;
    @Autowired
    private HomeDao homeDao;
    @Autowired
    private FlashPromotionMapper flashPromotionMapper;
//    @Autowired
//    private FlashPromotionSessionMapper promotionSessionMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductCategoryMapper productCategoryMapper;
//    @Autowired
//    private CmsSubjectMapper subjectMapper;

    @Override
    public HomeContentResult content() {
        HomeContentResult result = new HomeContentResult();
        //获取首页广告
        result.setAdvertiseList(getHomeAdvertiseList());
//        //获取推荐品牌
//        result.setBrandList(homeDao.getRecommendBrandList(0,6));
        //获取秒杀信息
        result.setHomeFlashPromotion(getHomeFlashPromotion());
        //获取新品推荐
//        result.setNewProductList(homeDao.getNewProductList(0,4));
        //获取推荐
        result.setRecommendProductList(recommendProductList(10,1));
        //获取热销
        result.setHotProductList(homeDao.getHotProductList(10));

        result.setProductList(productList(18,1));
//        //获取推荐专题
//        result.setSubjectList(homeDao.getRecommendSubjectList(0,4));
        return result;
    }



    @Override
    public List<Product> recommendProductList(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        ProductExample example = new ProductExample();
        example.createCriteria()
                .andIsdeleteEqualTo(0)
                .andPublishStatusEqualTo(1)
                .andRecommandStatusEqualTo(1);
        return productMapper.selectByExample(example);
    }

    @Override
    public List<ProductCategory> getProductCateList(Long parentId) {
        ProductCategoryExample example = new ProductCategoryExample();
        example.createCriteria()
                .andShowStatusEqualTo(1)
                .andParentIdEqualTo(parentId);
        example.setOrderByClause("sort desc");
        return productCategoryMapper.selectByExample(example);
    }

//    @Override
//    public List<CmsSubject> getSubjectList(Long cateId, Integer pageSize, Integer pageNum) {
//        PageHelper.startPage(pageNum,pageSize);
//        CmsSubjectExample example = new CmsSubjectExample();
//        CmsSubjectExample.Criteria criteria = example.createCriteria();
//        criteria.andShowStatusEqualTo(1);
//        if(cateId!=null){
//            criteria.andCategoryIdEqualTo(cateId);
//        }
//        return subjectMapper.selectByExample(example);
//    }

//    @Override
//    public List<Product> hotProductList(Integer pageNum, Integer pageSize) {
//        int offset = pageSize * (pageNum - 1);
//        return homeDao.getHotProductList(offset, pageSize);
//    }

    @Override
    public List<Product> hotProductList() {
        return homeDao.getHotProductList(10);
    }

    @Override
    public List<Product> productList(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        ProductExample productExample = new ProductExample();
        productExample.createCriteria().andIsdeleteEqualTo(0).andPublishStatusEqualTo(1);
        List<Product> products = productMapper.selectByExample(productExample);
        return products;
    }

//    @Override
//    public List<Product> newProductList(Integer pageNum, Integer pageSize) {
//        int offset = pageSize * (pageNum - 1);
//        return homeDao.getNewProductList(offset, pageSize);
//    }

    private List<HomeFlashPromotion> getHomeFlashPromotion() {
        List<HomeFlashPromotion> homeFlashPromotions = new ArrayList<>();
        //获取当前秒杀活动
        Date now = new Date();
        List<FlashPromotion> flashPromotions = getFlashPromotion(now);
        if(!Objects.isNull(flashPromotions) || flashPromotions.size() > 0){
            for (FlashPromotion f: flashPromotions) {
                HomeFlashPromotion homeFlashPromotion = new HomeFlashPromotion();
                homeFlashPromotion.setStartTime(f.getStartat());
                homeFlashPromotion.setEndTime(f.getEndat());
                //获取秒杀商品
                List<FlashPromotionProduct> flashProductList = homeDao.getFlashProductList(f.getId());
                homeFlashPromotion.setProductList(flashProductList);
            }
        }
//        if (flashPromotion != null) {
//            //获取当前秒杀场次
//            SmsFlashPromotionSession flashPromotionSession = getFlashPromotionSession(now);
//            if (flashPromotionSession != null) {
//                homeFlashPromotion.setStartTime(flashPromotionSession.getStartTime());
//                homeFlashPromotion.setEndTime(flashPromotionSession.getEndTime());
//                //获取下一个秒杀场次
//                SmsFlashPromotionSession nextSession = getNextFlashPromotionSession(homeFlashPromotion.getStartTime());
//                if(nextSession!=null){
//                    homeFlashPromotion.setNextStartTime(nextSession.getStartTime());
//                    homeFlashPromotion.setNextEndTime(nextSession.getEndTime());
//                }
//                //获取秒杀商品
//                List<FlashPromotionProduct> flashProductList = homeDao.getFlashProductList(flashPromotion.getId(), flashPromotionSession.getId());
//                homeFlashPromotion.setProductList(flashProductList);
//            }
//        }
        return homeFlashPromotions;
    }

    //获取下一个场次信息
//    private SmsFlashPromotionSession getNextFlashPromotionSession(Date date) {
//        SmsFlashPromotionSessionExample sessionExample = new SmsFlashPromotionSessionExample();
//        sessionExample.createCriteria()
//                .andStartTimeGreaterThan(date);
//        sessionExample.setOrderByClause("start_time asc");
//        List<SmsFlashPromotionSession> promotionSessionList = promotionSessionMapper.selectByExample(sessionExample);
//        if (!CollectionUtils.isEmpty(promotionSessionList)) {
//            return promotionSessionList.get(0);
//        }
//        return null;
//    }

    private List<HomeAdvertise> getHomeAdvertiseList() {
        HomeAdvertiseExample example = new HomeAdvertiseExample();
        example.createCriteria().andTypeEqualTo(1).andStatusEqualTo(1);
        example.setOrderByClause("sort desc");
        return advertiseMapper.selectByExample(example);
    }

    //根据时间获取秒杀活动
    private List<FlashPromotion> getFlashPromotion(Date date) {
        Date currDate = DateUtil.getDate(date);
        FlashPromotionExample example = new FlashPromotionExample();
        example.createCriteria()
                .andStatusEqualTo(1)
                .andStartatLessThanOrEqualTo(currDate)
                .andEndatGreaterThanOrEqualTo(currDate);
        List<FlashPromotion> flashPromotionList = flashPromotionMapper.selectByExample(example);
        return flashPromotionList;
//        if (!CollectionUtils.isEmpty(flashPromotionList)) {
//            return flashPromotionList.get(0);
//        }
//        return null;
    }

    //根据时间获取秒杀场次
//    private SmsFlashPromotionSession getFlashPromotionSession(Date date) {
//        Date currTime = DateUtil.getTime(date);
//        SmsFlashPromotionSessionExample sessionExample = new SmsFlashPromotionSessionExample();
//        sessionExample.createCriteria()
//                .andStartTimeLessThanOrEqualTo(currTime)
//                .andEndTimeGreaterThanOrEqualTo(currTime);
//        List<SmsFlashPromotionSession> promotionSessionList = promotionSessionMapper.selectByExample(sessionExample);
//        if (!CollectionUtils.isEmpty(promotionSessionList)) {
//            return promotionSessionList.get(0);
//        }
//        return null;
//    }
}
